#define BLINKER_WIFI
#define BLINKER_ALIGENIE_LIGHT
#include <Blinker.h>
// 修改为自己的
char auth[] = "4cd3ef11*****";
//自家的wifi和pwd
char ssid[] = "TP-LINK_3211";
char pswd[] = "******";
bool oState = false;

// 新建组件对象
BlinkerButton Button1("btn-light");
BlinkerNumber Number1("num-light");

int counter = 0;

// 按下按键即会执行该函数
void button1_callback(const String & state)
{
  BLINKER_LOG("get button state: ", state);
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  digitalWrite(0, !digitalRead(0));
}


void aligeniePowerState(const String & state)
{
  BLINKER_LOG("need set power state: ", state);

  if (state == BLINKER_CMD_ON) {
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(0, LOW);
    BlinkerAliGenie.powerState("on");
    BlinkerAliGenie.print();
  }
  else if (state == BLINKER_CMD_OFF) {
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(0, HIGH);
    BlinkerAliGenie.powerState("off");
    BlinkerAliGenie.print();
  }
}

// 如果未绑定的组件被触发，则会执行其中内容
void dataRead(const String & data)
{
  BLINKER_LOG("Blinker readString: ", data);
  counter++;
  Number1.print(counter);
}

void setup()
{
  // 初始化串口
  Serial.begin(115200);
  BLINKER_DEBUG.stream(Serial);
  BLINKER_DEBUG.debugAll();

  // 初始化有LED的IO
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH);
  // 初始化blinker
  Blinker.begin(auth, ssid, pswd);
  Blinker.attachData(dataRead);

  Button1.attach(button1_callback);
  BlinkerAliGenie.attachPowerState(aligeniePowerState);
  BlinkerAliGenie.attachQuery(aligenieQuery);
}

void loop() {
  Blinker.run();
}

void aligenieQuery(int32_t queryCode)
{
  BLINKER_LOG("AliGenie Query codes: ", queryCode);

  switch (queryCode)
  {
    case BLINKER_CMD_QUERY_ALL_NUMBER :
      BLINKER_LOG("AliGenie Query All");
      BlinkerAliGenie.powerState(oState ? "on" : "off");
      BlinkerAliGenie.print();
      break;
    case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
      BLINKER_LOG("AliGenie Query Power State");
      BlinkerAliGenie.powerState(oState ? "on" : "off");
      BlinkerAliGenie.print();
      break;
    default :
      BlinkerAliGenie.powerState(oState ? "on" : "off");
      BlinkerAliGenie.print();
      break;
  }
}